package controllers

import (
	"CRMSystem/models"
)

type CustomerViewController struct {
	MainController
}

// GetCustomer 客户详细信息
func (c *CustomerViewController) GetCustomer() {
	c.IfHasSession()
	id := GetSessionInt(c.GetSession("customerID"))
	customer, err := models.GetCustomerViewInformationByCustomerID(id)
	if err == nil {
		c.Data["json"] = Status{Date: customer, ErrCode: GET_OK, Msg: "CustomerList:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// GetServiceList 服务列表
func (c *CustomerViewController) GetServiceList() {
	c.IfHasSession()
	id := GetSessionInt(c.GetSession("customerID"))
	services, i := models.GetServiceListInformationByCustomerID(id)
	if i > 0 {
		c.Data["json"] = Status{Date: services, ErrCode: GET_OK, Msg: "CustomerList:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}

// GetCustomerInfoByCustomerID //按id找客户信息
func (c *CustomerViewController) GetCustomerInfoByCustomerID() {
	c.IfHasSession()
	id, _ := c.GetInt("customerID")
	customer, err := models.GetCustomerViewInformationByCustomerID(id)
	if err == nil {
		c.Data["json"] = Status{Date: customer, ErrCode: GET_OK, Msg: "GetCustomerInfoByCustomerID:success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "Error"}
		c.ServeJSON()
		c.Redirect("/page/err400", 302)
	}
}
